package textanalyzer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.StringTokenizer;

import com.sun.org.apache.bcel.internal.generic.NEW;

public class MainAnalyzer {
	
	private static ArrayList<String> Neglections = new ArrayList<String>();
	private static HashMap<String, Integer> DBmap = new HashMap<String, Integer>();
	private static String  AddInfo;
	
	public MainAnalyzer() {
		fileReader newReader = new fileReader();
		ArrayList<String> tempNeglections = newReader.ReadFile("D:\\project workspace\\picSEEK\\XMLRepository\\Neglections.txt");
		Neglections.addAll(tempNeglections);
		AddInfo = "";
	}
	
	/*public static void main(String[] args) {
		
		Neglections = new ArrayList<String>();
		Neglections.add("for");
		Neglections.add("by");
		Neglections.add("to");
		Neglections.add("do");
		Neglections.add("while");
		Neglections.add("is");
		Neglections.add("as");
		Neglections.add("the");
		Neglections.add("it");
		Neglections.add("a");
		Neglections.add("an");
		Neglections.add("was");
		Neglections.add("and");
		Neglections.add("of");
		Neglections.add("some");
		Neglections.add("though");
		Neglections.add("typically");
		Neglections.add("through");
		Neglections.add("other");
		Neglections.add("or");
		Neglections.add("in");
		Neglections.add("bedside");
		Neglections.add("actual");
		Neglections.add("word");
		Neglections.add("ordinary");
		Neglections.add("are");
		
		fileReader newReader = new fileReader();
		ArrayList<String> tempNeglections = newReader.ReadFile("D:\\Private desktops\\Kulz\\picSEEK\\picSEEK Workspace\\TextAnalyzer_0.0\\Neglections.txt");
		Neglections.addAll(tempNeglections);
		
		//String AddInfo = "Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation.) ";
		AddInfo = "Medicine is the art and science of healing. It encompasses a range of health care practices evolved to maintain and restore health by the prevention and treatment of illness.Contemporary medicine applies health science, biomedical research, and medical technology to diagnose and treat injury and disease, typically through medication, surgery, or some other form of therapy. The word medicine is derived from the Latin ars medicina, meaning the art of healing.Though medical technology and clinical expertise are pivotal to contemporary medicine, successful face-to-face relief of actual suffering continues to require the application of ordinary human feeling and compassion, known in English as bedside manner.";
		//String  AddInfo = "Engineering is the discipline, art and profession of acquiring and applying technical, scientific and mathematical knowledge to design and implement materials, structures, machines, devices, systems, and processes that safely realize a desired objective or inventions.The American Engineers' Council for Professional Development (ECPD, the predecessor of ABET[1]) has defined engineering as follows:[T]he creative application of scientific principles to design or develop structures, machines, apparatus, or manufacturing processes, or works utilizing them singly or in combination; or to construct or operate the same with full cognizance of their design; or to forecast their behavior under specific operating conditions; all as respects an intended function, economics of operation and safety to life and property.One who practices engineering is called an engineer, and those licensed to do so may have more formal designations such as Professional Engineer, Chartered Engineer, Incorporated Engineer, or European Engineer. The broad discipline of engineering encompasses a range of more specialized subdisciplines, each with a more specific emphasis on certain fields of application and particular areas of technology.";
		ArrayList<String> Words = Tokenizer(AddInfo);
		DBmap = generateMap(Words);
		
		
		//Iterator< String> Twords = DBmap.keySet().iterator();		
		MapSorter nMapSorter = new MapSorter(DBmap);
		LinkedHashMap<String, Integer> lMap = nMapSorter.sortMap();
		Iterator< String> Twords = lMap.keySet().iterator();				
				while (Twords.hasNext()) {
					String currWord =  Twords.next();
					System.out.print(currWord);
					System.out.print(" = ");
					System.out.print(lMap.get(currWord).toString());
					System.out.print("\n");
				}	
			
		}*/
		
	public LinkedHashMap<String, Integer> identifyContext(String rawText) {
		LinkedHashMap<String, Integer> tagVector = new LinkedHashMap<String, Integer>();
		AddInfo = rawText;
		ArrayList<String> Words = Tokenizer(AddInfo);
		DBmap = generateMap(Words);
		MapSorter nMapSorter = new MapSorter(DBmap);
		tagVector= nMapSorter.sortMap();
		
		
		return tagVector;
	}
	
	private static ArrayList<String> Factorizer(String AddInfo) 
	{
		ArrayList<String> Words = new ArrayList<String>();
		String tmpword = ""; 
		int nxtBreak = AddInfo.indexOf(" ");
			
		while(!AddInfo.isEmpty() && nxtBreak != -1)
		{	
		tmpword = AddInfo.substring(0,nxtBreak);
		tmpword = tmpword.toLowerCase();
		
		AddInfo = AddInfo.substring(nxtBreak);
		AddInfo = AddInfo.trim();
		
		
		if(!Neglections.contains(tmpword))Words.add(tmpword);
		nxtBreak = AddInfo.indexOf(" ");
		
		//if(nxtBreak > AddInfo.indexOf(","))nxtBreak = AddInfo.indexOf(",");
		if(nxtBreak > AddInfo.indexOf("."))
			{
			nxtBreak = AddInfo.indexOf(".") + 1;
			}
		//if(nxtBreak == -1)nxtBreak = AddInfo.indexOf("/");
		//if(nxtBreak == -1)nxtBreak = AddInfo.indexOf("-");
		
		}
		return Words;
	}
	
	public static ArrayList<String> Tokenizer(String addInfo) {
		ArrayList<String> stringTokens = new ArrayList<String>();
		StringTokenizer tokenizer = new StringTokenizer(addInfo, " \t\n\r\f-_;:,.'()");
		fileReader nFileReader = new fileReader();
		ArrayList<String> negTags = nFileReader.ReadFile("D:\\project workspace\\picSEEK\\XMLRepository\\Neglections.txt");
		while (tokenizer.hasMoreTokens()) {
			String tempToken = tokenizer.nextToken();
			//if(!Neglections.contains(tempToken))stringTokens.add(tempToken.toLowerCase());
			if(!negTags.contains(tempToken))stringTokens.add(tempToken.toLowerCase());
		}		
		return stringTokens;
	}
	
	public static HashMap<String, Integer> generateMap(ArrayList<String> tokens) {
		HashMap<String, Integer> tokenVector = new HashMap<String, Integer>();
		Iterator<String> iter = tokens.iterator();
		while (iter.hasNext()) {
			String tempToken = (String) iter.next();
			if(tokenVector.containsKey(tempToken))tokenVector.put(tempToken, (Integer)tokenVector.get(tempToken)+1);
			else tokenVector.put(tempToken, 1);
		}
		return tokenVector;
	}
}
